package com.gary;

import sun.java2d.opengl.OGLContext;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 1
 * 有组织机构表org如下：
 * id   name   parent_id
 *
 * ----------------------------
 *
 * 1     A      NULL
 *
 * 2     B      1
 *
 * 3     C      5
 *
 * 4     E      NULL
 *
 * 5     F      2
 *
 * 6     G     4
 *
 * 子机构的 parent_id 为其父级机构的 id，该表对应的实体类为 Org，补全transferOrgBo方法的代码实现将 Org类型列表转换为完整组织路径的 OrgBo类型。 (备注：输入orgs 为数据库查询出来的完备的机构集)
 *
 * 备：可以添加类、方法 辅助实现逻辑
 *
 * 组织路径:从当前的组织节点到根节点的路径，用/分割
 *
 * 举例：
 *
 * A的组织路径是 A
 *
 * B的组织路径是 A/B
 *
 * C的组织路径是 A/B/F/
 * @author peng_rui
 * @date 2022/9/29
 */
public class MeiTuanInterviewQuestion1 {

    private static List<Org> orgList = new ArrayList<>();
    private static Map<Long, Org> orgMap = new HashMap<>();

    static {
        Org a = new Org(1L, "A", null);
        Org b = new Org(2L, "B", 1L);
        Org c = new Org(3L, "C", 5L);
        Org e = new Org(4L, "E", null);
        Org f = new Org(5L, "F", 2L);
        Org g = new Org(6L, "G", 4L);
        orgList.add(a);
        orgList.add(b);
        orgList.add(c);
        orgList.add(e);
        orgList.add(f);
        orgList.add(g);

        orgMap = orgList.stream().collect(Collectors.toMap(Org::getId, org -> org));
    }

    public static void main(String[] args) {
        List<OrgBo> orgBoList = transferOrgBo(orgList);
        System.out.println(orgBoList);
    }


    /**
     * 转换核心代码
     * @param orgs
     * @return
     */
    static List<OrgBo> transferOrgBo(List<Org> orgs) {
        List<OrgBo> orgBoList = new ArrayList<>();
        orgs.forEach(org -> {
            OrgBo orgBo = new OrgBo();
            orgBo.setId(org.getId());
            orgBo.setName(org.getName());
            orgBo.setParentId(org.getParentId());
            String path = genPath(org);
            String realPath = new StringBuilder(path).reverse().toString();
            orgBo.setPath(realPath);
            orgBoList.add(orgBo);
        });
        return orgBoList;
    }

    /**
     * 生成path
     * @param org
     * @return
     */
    static String genPath(Org org) {
        if (org.getParentId() == null) {
            return org.getName();
        }
        Org parentOrg = orgMap.get(org.getParentId());
        String path = org.getName() + "/" + genPath(parentOrg);
        return path;
    }
}

class Org {

    private Long id;

    private String name;

    private Long parentId;



    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Org org = (Org) o;
        return Objects.equals(id, org.id) && Objects.equals(name, org.name) && Objects.equals(parentId, org.parentId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, parentId);
    }

    public Org() {
    }

    public Org(Long id, String name, Long parentId) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
    }
}

class OrgBo {

    private Long id;

    private String name;

    private Long parentId;

    private String path;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OrgBo orgBo = (OrgBo) o;
        return Objects.equals(id, orgBo.id) && Objects.equals(name, orgBo.name) && Objects.equals(parentId, orgBo.parentId) && Objects.equals(path, orgBo.path);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, parentId, path);
    }

    public OrgBo() {
    }

    public OrgBo(Long id, String name, Long parentId, String path) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
        this.path = path;
    }
}